package org.jeecg.modules.api.wlhy.losssetting;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerService;
import org.jeecg.modules.wlhy.losssetting.entity.HyLossProportion;
import org.jeecg.modules.wlhy.losssetting.service.IHyLossProportionService;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: TODO
 * @author: luruirui
 * @date: 2021年04月14日 14:03
 */
@Api(tags="APP亏损比例设置")
@RestController
@RequestMapping("/api/wlhy/lossProportion")
@Slf4j
public class ApiLossProportionController {
    @Autowired
    private IHyLossProportionService hyLossProportionService;
    @Autowired
    private IHyCustomerService hyCustomerService;
    @Autowired
    private UserUtil userUtil;

    /**
     * 分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value="亏损免赔比例表-分页列表查询", notes="亏损免赔比例表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<HyLossProportion> queryWrapper = QueryGenerator.initQueryWrapper(new HyLossProportion(), req.getParameterMap());

        LoginUser loginUser = UserUtil.getLoginUser();
        String userId = loginUser.getId();
        QueryWrapper<HyCustomer> customerWrapper = QueryGenerator.initQueryWrapper(new HyCustomer(), req.getParameterMap());
        customerWrapper.eq("user_id", userId);
        HyCustomer customer = hyCustomerService.getOne(customerWrapper);
        if(customer == null){
            return Result.error("货主信息不存在!");
        }

        queryWrapper.eq("customer_id",customer.getId());
        Page<HyLossProportion> page = new Page<HyLossProportion>(pageNo, pageSize);
        IPage<HyLossProportion> pageList = hyLossProportionService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     *   添加
     *
     * @param jsonObject
     * @param req
     * @return
     */
    @AutoLog(value = "亏损免赔比例表-添加")
    @ApiOperation(value="亏损免赔比例表-添加", notes="亏损免赔比例表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody JSONObject jsonObject, HttpServletRequest req) {
        LoginUser loginUser = UserUtil.getLoginUser();
        String userId = loginUser.getId();
        QueryWrapper<HyCustomer> wrapper = QueryGenerator.initQueryWrapper(new HyCustomer(), req.getParameterMap());
        wrapper.eq("user_id", userId);
        HyCustomer customer = hyCustomerService.getOne(wrapper);
        if(customer == null){
            return Result.error("货主信息不存在!");
        }

        double lossProportion = jsonObject.getDouble("lossProportion");
        String lossType = jsonObject.getString("lossType");
        String goodsName = jsonObject.getString("goodsName");

        HyLossProportion hyLossProportion = new HyLossProportion();
        hyLossProportion.setCustomerId(customer.getId());
        hyLossProportion.setLossProportion(lossProportion);
        hyLossProportion.setLossType(lossType);
        hyLossProportion.setGoodsName(goodsName);
        hyLossProportionService.save(hyLossProportion);
        return Result.OK("添加成功！");
    }

    /**
     *  编辑
     *
     * @param hyLossProportion
     * @return
     */
    @AutoLog(value = "亏损免赔比例表-编辑")
    @ApiOperation(value="亏损免赔比例表-编辑", notes="亏损免赔比例表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody HyLossProportion hyLossProportion) {
        hyLossProportionService.updateById(hyLossProportion);
        return Result.OK("编辑成功!");
    }

    /**
     *   通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "亏损免赔比例表-通过id删除")
    @ApiOperation(value="亏损免赔比例表-通过id删除", notes="亏损免赔比例表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        HyLossProportion lossProportion = hyLossProportionService.getById(id);
        if(lossProportion == null){
            return Result.error("数据不存在!");
        }
        hyLossProportionService.removeById(id);
        return Result.OK("删除成功!");
    }
}
